<template>
  <div class="departments-container">
    <el-card class="tree-card">
      <tree-tools :tree-node="company" :is-root="true"></tree-tools>
      <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
        <tree-tools @addDepts="addDepts" @delDepts="getDepartments" slot-scope="{ data }" :tree-node="data" :is-root="false"></tree-tools>
      </el-tree>
    </el-card>
    <add-dept @addDepts="getDepartments" :show-dialog="showDialog" @closeDK="closeD" :tree-node="node" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept'
export default {
  name: 'DepartMents',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [
        // { name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        // { name: '行政部', manager: '刘备' },
        // { name: '人事部', manager: '孙权' }
      ],
      defaultProps: {
        label: 'name'
      },
      company: { name: '北京kkyy科技股份有限公司', manager: '负责人' },
      showDialog: false,
      node: null
    }
  },

  mounted() {
    this.getDepartments()
  },

  methods: {
    async getDepartments() {
      const { data: res } = await getDepartments()
      this.departs = tranListToTreeData(res.data.depts, '')
    },
    addDepts(node) {
      console.log('8888888')
      this.showDialog = true
      this.node = node
    },
    closeD() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 80px;
  font-size: 14px;
}
</style>
